package shuiyuqing.network.http;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.xkh.xkh.R;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xkh.xkh.MyApplication;
import managers.VolleySingleton;
import shuiyuqing.network.beans.IrrBMSystem;
import shuiyuqing.network.beans.RealTimeIrrBRWaterCannal;

/**
 * 获取实时渠道信息，此方法有逻辑问题，不推荐使用。
 * Created by 凯 on 2015/11/6.
 */
public class GetBRWaterChannel {


    // 测站编码
    String stadiacode;

    // 下载
    Download download;

    public GetBRWaterChannel(String stadiacode) {
        this.stadiacode = stadiacode;
    }

    // 获取
    public void get() {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/guanqu/GetBRWaterChannel";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        afterGet(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                map.put("data", stadiacode);
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    // 获取结果
    private void afterGet(String response) {
        download = JSON.parseObject(response,Download.class);

        // 若获取结果成功
        if (download.getStatus() == DownloadInfo.SUCCESS)
        {

        }else if (download.getStatus() == DownloadInfo.FAILED)
        {

        }
    }


    // 下行數據
    public static class Download {
        int status;
        List<RealTimeIrrBRWaterCannal> cannals;
        List<IrrBMSystem> systems;

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public List<RealTimeIrrBRWaterCannal> getCannals() {
            return cannals;
        }

        public void setCannals(List<RealTimeIrrBRWaterCannal> cannals) {
            this.cannals = cannals;
        }

        public List<IrrBMSystem> getSystems() {
            return systems;
        }

        public void setSystems(List<IrrBMSystem> systems) {
            this.systems = systems;
        }


    }
}
